import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

class StorageService extends GetxService {
  static StorageService get to => Get.find();

  late final SharedPreferences _prefs;
  static const String _tokenKey = 'token';
  static const String _userNameKey = 'userName';

  Future<StorageService> init() async {
    _prefs = await SharedPreferences.getInstance();
    return this;
  }

  Future<void> setToken(String token) async {
    await _prefs.setString(_tokenKey, token);
  }

  String? getToken() {
    return _prefs.getString(_tokenKey);
  }

  Future<void> clearToken() async {
    await _prefs.remove(_tokenKey);
  }

  Future<void> setUserName(String userName) async {
    await _prefs.setString(_userNameKey, userName);
  }

  String? getUserName() {
    return _prefs.getString(_userNameKey);
  }
}
